#include "VxIrrLib.h"

//------------------------------------------------------------------------------
EntCometIce::EntCometIce( VxIrrBase *	poIrrBase, const EntSpawnData& oEntSpawnData )
	: EntBase( poIrrBase )
	, m_poParticles(0)
{
	log_msg( 0, "EntCometIce::EntCometIce creating entity\n" );
	SetEntType( eEntityTypeCometIce );
	CreateEntity( oEntSpawnData );
	RebuildParticleSystem();
}

//------------------------------------------------------------------------------
//! override EntBase
//! handle collision with another object
//! return non zero if caller should not process any more contact points
RCODE   EntCometIce::HandleCollision( EntBase * poOtherEntity, const btPersistentManifold * poContactManifold, float f32NormalScale )
{
	switch( poOtherEntity->m_eEntType )
	{
	case eEntityTypeCometFire:
		log_msg( 0, "Collision Comet Ice <-> Comet Fire\n" );
		break;
	case eEntityTypeCometIce:
		log_msg( 0, "Collision Comet Ice <-> Comet Ice\n" );
		break;
	case eEntityTypeAsteroidRed:
		log_msg( 0, "Collision Comet Ice <-> Asteroid Red\n" );
		break;
	case eEntityTypeAsteroidBlue:
		log_msg( 0, "Collision Comet Ice <-> Asteroid Blue\n" );
		switch( m_poIrrBase->m_iGameLevel )
		{
		case 1:
			OnPowerLevelUp( 1, 60 );
			break;
		case 2:
			OnPowerLevelUp( 1, 40 );
			break;
		case 3:
			OnPowerLevelUp( 1, 20 );
			break;
		default:
			break;
		}
		m_poIrrBase->ShowTimedMessage( m_poSceneNode->getPosition(), "POWER UP", 2 );
		break;

	case eEntityTypeEarth:
		log_msg( 0, "Collision Comet Ice <-> Earth\n" );
		break;
	default:
		log_msg( 0, "Collision Comet Ice <-> UNKOWN\n" );
		break;
	}
	return 0;
}
//------------------------------------------------------------------------------
//! called once per second
void EntCometIce::OncePerSecond( void )
{
	if( this->GetPowerLevel() > 0 )
	{
		vx_assert( this->m_s32PowerUpTTL )
		this->m_s32PowerUpTTL--;
		if( 0 == this->m_s32PowerUpTTL )
		{
			m_s32PowerLevel = 0;
			OnPowerLevelDown();
		}
	}
}
//------------------------------------------------------------------------------
//! handle changes when power level goes up
void EntCometIce::OnPowerLevelUp( S32 s32PowerLevel, S32 s32PowerUpTTL )
{
	m_s32PowerLevel = s32PowerLevel;
	this->m_s32PowerUpTTL = s32PowerUpTTL;
	this->m_poSceneNode->setScale( irr::core::vector3df( 2.0f, 2.0f, 2.0f ));
	RebuildParticleSystem();
}

//------------------------------------------------------------------------------
//! handle changes when power level goes down
void EntCometIce::OnPowerLevelDown( void )
{
	this->m_poSceneNode->setScale( irr::core::vector3df( 1.0f, 1.0f, 1.0f ));
	RebuildParticleSystem();
}

//------------------------------------------------------------------------------
//! rebuild particle system
void EntCometIce::RebuildParticleSystem( void )
{
	if( m_poParticles )
	{
		this->m_poSceneNode->removeChild( m_poParticles );
	}
	if( m_s32PowerLevel )
	{
		m_poParticles = m_poIrrBase->AddCometTrailParticleSystem( this->m_poSceneNode, "iceball.png", 1.3f );
	}
	else
	{
		m_poParticles = m_poIrrBase->AddCometTrailParticleSystem( this->m_poSceneNode, "iceball.png", 1.0f );
	}
}

//------------------------------------------------------------------------------
//! override EntBase
//! return true is scene node is this entities or any children like particle system etc
BOOL EntCometIce::IsMySceneNode( irr::scene::ISceneNode * poNode )
{
	if( poNode == m_poSceneNode )
	{
		return true;
	}
	if(  poNode == m_poParticles )
	{
		return true;
	}
	return false;
}



